package com.gopay.checkorder.service.task.thread.batch;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;


import org.apache.commons.lang3.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BcdlOrderSendBankService;
import com.gopay.checkorder.service.sentinel.BcdlOrderSentinel;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.common.domain.bank.bcdl.BcdlPacketRes;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.common.util.spring.SpringContextUtil;
import com.gopay.gass.service.GassPayRemoteService;

public class CheckIcbcOrderBcdlThread implements Runnable {

	private Logger logger = LoggerFactory.getLogger(CheckIcbcOrderBcdlThread.class);

	private BcdlOrderSentinel sentinel;
	
	 private GassPayRemoteService gassPayService;

	private String bank;

	private CountDownLatch countDownLatch;

	private String packetNo;

	private String label;
	
	private void printLog(StopWatch sw, String bank, String prefix) {
		sw.split();
		logger.error(prefix + "------,bank=" + bank+  ",耗时="
				+ sw.getSplitTime());
	}
	

	public CheckIcbcOrderBcdlThread(String label, String bank, String packetNo, CountDownLatch countDownLatch, BcdlOrderSentinel sentinel, GassPayRemoteService gassPayService) {
		this.label = label;
		this.bank = bank;
		this.packetNo = packetNo;
		this.countDownLatch = countDownLatch;
		this.sentinel = sentinel;
		this.gassPayService = gassPayService;
	}

	@Override
	public void run() {

		BcdlPacketRes res = new BcdlPacketRes();
		StopWatch sw = new StopWatch();
		sw.start();
		printLog(sw, bank,"CheckIcbcOrderBcdlThread------start,packetNo="+packetNo);
		
		
		try {
			String beanName = getSendBankBeanName(bank);

			if (false == SpringContextUtil.containsBean(beanName)) {
				logger.info("spring bean does not exist: " + beanName);
				countDownLatch.countDown();
				return;
			}

			BcdlOrderSendBankService sendBankService = (BcdlOrderSendBankService) SpringContextUtil.getBean(beanName);

			HttpTransport notifyGass = new HttpTransport();
			notifyGass.setSendEncoding("UTF-8");

			logger.info("线程池中处理数据开始packetNo=" + packetNo);

			try {
				logger.error("多线程任务银企直连 对账--开始 ，packetNo=" + packetNo + ",bank=" + bank);
				res = sendBankService.query4batch(packetNo, label, bank);
				logger.error("多线程任务银企直连 对账--结束 ，packetNo=" + packetNo + ",bank=" + bank + ",resultCode=" + res.getResultCode());

				if (ProcCodeConstants.PROC_CODE_100S1000.equals(res.getResultCode())) {
					for (BcdlResult bcdlResult : res.getBcdlResults()) {
						Map<String, String> param = new HashMap<String, String>();
						if (ProcCodeConstants.PROC_CODE_100E6027.equals(bcdlResult.getResultCode())) {
							param.put("resultCode", "2");
							param.put("resultMsg", bcdlResult.getErrorMsg());
							param.put("remark", "1");
							param.put("gopayOrderId", bcdlResult.getGopayOrderId() );
							//add by zyt,2017-10-13 ,是否需要做失败的拒绝账务，
							if(bcdlResult.isNeedRefund()){
								logger.info("发起失败拒绝账务：orderId:" + bcdlResult.getGopayOrderId());
								param.put("needRefund", "1");
							}
							/*notifyGass.setUrl(bankOrder.getRemark2());
							notifyGass.submit(param);*/ //生产优化10期 lx httpclient 替换为 hessian
		                    gassPayService.bcdlNotifyGass(param);
						} else if (ProcCodeConstants.PROC_CODE_100S1000.equals(bcdlResult.getResultCode())) {
							param.put("resultCode", "1");
							param.put("remark", "1");
							param.put("gopayOrderId", bcdlResult.getGopayOrderId());
							/*notifyGass.setUrl(bankOrder.getRemark2());
							notifyGass.submit(param);*/ //生产优化10期 lx httpclient 替换为 hessian
		                    gassPayService.bcdlNotifyGass(param);
						} else {
							param.put("resultCode", "3");
							param.put("remark", "1");
							param.put("gopayOrderId", bcdlResult.getGopayOrderId());
							/*notifyGass.setUrl(bankOrder.getRemark2());
							notifyGass.submit(param);*/ //生产优化10期 lx httpclient 替换为 hessian
		                    gassPayService.bcdlNotifyGass(param);
						}
					}
				}
				
				

			} catch (Exception e) {
				logger.error("多线程任务批量对账异常 ，packetNo=" + packetNo + ",bank=" + bank, e);
			} finally {
				logger.error("--------调用熔断器开始---------");
				sentinel.batchCheckPatrol(packetNo, res);
				logger.error("--------调用熔断器结束---------");
			}

		} catch (GopayException e) {
			logger.error("调用bis异常packetNo=" + packetNo, e);

		} catch (Exception e) {
			logger.error("调用bis异常packetNo=" + packetNo, e);

		} finally {
			countDownLatch.countDown();
			printLog(sw, bank,"CheckIcbcOrderBcdlThread------end,packetNo="+packetNo);
			sw.stop();
		}
	}

	private String getSendBankBeanName(String bankCode) throws GopayException {
		String beanName = bankCode + "-BCDL";
		return beanName;
	}


}
